package main

import (
	"fmt"
	"sync"
)

// 这是一个key为字符串类型，value为数字的字典
type StrIntMap struct {
	m sync.Map
}

func (ms *StrIntMap) Delete(key int) {
	ms.m.Delete(key)
}

func (ms *StrIntMap) Load(key int) (value int, ok bool) {
	v, ok := ms.m.Load(key)
	if v != nil {
		value = v.(int)
	}
	return
}

func (ms *StrIntMap) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) {
	a, loaded := ms.m.LoadOrStore(key, value)
	actual = a.(int)
	return
}

func (ms *StrIntMap) Range(f func(key string, value int) bool) {
	f1 := func(key, value interface{}) bool {
		return f(key.(string), value.(int))
	}
	ms.m.Range(f1)
}

func (ms *StrIntMap) Store(key string, value int) {
	ms.m.Store(key, value)
}

func main() {
	var m1 StrIntMap
	m1.Store("k1", 1)
	m1.Store("k2", 2)
	m1.m.Range(func(k, v interface{}) bool {
		fmt.Printf("%v: %v\n", k, v)
		return true
	})
}
